'use client'

import { Fragment, useState } from 'react'
import { Dialog, Transition } from '@headlessui/react'
import { XMarkIcon } from '@heroicons/react/24/outline'

interface TextRewriteModalProps {
  isOpen: boolean
  onClose: () => void
  onRewrite: (options: RewriteOptions) => void
  selectedCount: number
}

interface RewriteOptions {
  prompt: string
}

export default function TextRewriteModal({ isOpen, onClose, onRewrite, selectedCount }: TextRewriteModalProps) {
  const [options, setOptions] = useState<RewriteOptions>({
    prompt: ''
  })

  const [isProcessing, setIsProcessing] = useState(false)

  const handleSubmit = async () => {
    setIsProcessing(true)
    try {
      await onRewrite(options)
    } finally {
      setIsProcessing(false)
    }
  }

  const handleClose = () => {
    if (!isProcessing) {
      onClose()
    }
  }

  return (
    <Transition appear show={isOpen} as={Fragment}>
      <Dialog as="div" className="relative z-50" onClose={handleClose}>
        <Transition.Child
          as={Fragment}
          enter="ease-out duration-300"
          enterFrom="opacity-0"
          enterTo="opacity-100"
          leave="ease-in duration-200"
          leaveFrom="opacity-100"
          leaveTo="opacity-0"
        >
          <div className="fixed inset-0 bg-black bg-opacity-25" />
        </Transition.Child>

        <div className="fixed inset-0 overflow-y-auto">
          <div className="flex min-h-full items-center justify-center p-4 text-center">
            <Transition.Child
              as={Fragment}
              enter="ease-out duration-300"
              enterFrom="opacity-0 scale-95"
              enterTo="opacity-100 scale-100"
              leave="ease-in duration-200"
              leaveFrom="opacity-100 scale-100"
              leaveTo="opacity-0 scale-95"
            >
              <Dialog.Panel className="w-full max-w-2xl transform overflow-hidden rounded-2xl bg-white p-6 text-left align-middle shadow-xl transition-all">
                <div className="flex items-center justify-between mb-6">
                  <Dialog.Title as="h3" className="text-lg font-medium leading-6 text-gray-900">
                    批量改写文案 ({selectedCount} 个视频)
                  </Dialog.Title>
                  <button
                    onClick={handleClose}
                    disabled={isProcessing}
                    className="text-gray-400 hover:text-gray-600 disabled:opacity-50"
                  >
                    <XMarkIcon className="h-6 w-6" />
                  </button>
                </div>

                <div className="space-y-6">
                  {/* 自定义改写提示词 */}
                  <div>
                    <label htmlFor="prompt" className="block text-sm font-medium text-gray-700 mb-2">
                      改写提示词
                    </label>
                    <textarea
                      id="prompt"
                      value={options.prompt}
                      onChange={(e) => setOptions(prev => ({ ...prev, prompt: e.target.value }))}
                      placeholder="请输入改写要求，例如：&#10;- 改写成营销推广风格，语调要有说服力&#10;- 突出产品特色，增加情感色彩&#10;- 适合年轻人群体，语言要活泼有趣&#10;- 压缩内容长度，突出重点信息"
                      rows={8}
                      disabled={isProcessing}
                      className="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 disabled:bg-gray-50 text-sm"
                    />
                    <div className="mt-2 text-xs text-gray-500">
                      💡 提示：详细描述您的改写需求，AI会根据您的要求进行文案改写
                    </div>
                  </div>
                </div>

                {/* 操作按钮 */}
                <div className="flex justify-end space-x-3 mt-8">
                  <button
                    type="button"
                    onClick={handleClose}
                    disabled={isProcessing}
                    className="px-4 py-2 text-sm font-medium text-gray-700 bg-white border border-gray-300 rounded-md hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 disabled:opacity-50"
                  >
                    取消
                  </button>
                  <button
                    type="button"
                    onClick={handleSubmit}
                    disabled={isProcessing || !options.prompt.trim()}
                    className="px-4 py-2 text-sm font-medium text-white bg-indigo-600 border border-transparent rounded-md hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 disabled:opacity-50"
                  >
                    {isProcessing ? '改写中...' : `开始改写 (${selectedCount} 个)`}
                  </button>
                </div>
              </Dialog.Panel>
            </Transition.Child>
          </div>
        </div>
      </Dialog>
    </Transition>
  )
}
